package com.kll.carolinehmibiz.presentation.presenter

import android.content.Context
import com.google.gson.GsonBuilder
import com.google.gson.reflect.TypeToken
import com.kll.carolinehmibiz.OperationManager
import com.kll.carolinehmibiz.model.OperateListener
import com.kll.carolinehmibiz.net.bean.GetRegionResponse
import com.kll.carolinehmibiz.presentation.BasePresenter
import com.kll.carolinehmibiz.presentation.listener.AddressDetailProtocol
import com.kll.carolinehmibiz.base.utils.L

class AddressDetailPresenter(context: Context, mView: AddressDetailProtocol.IView): BasePresenter(context), AddressDetailProtocol.IPresenter {
    private var mView: AddressDetailProtocol.IView? = mView
    private var addAddressIsBusying = false
    private var editAddressIsBusying = false
    private var delAddressIsBusying = false
    private var getRegionIsBusying = false

    var provinces = ArrayList<GetRegionResponse>()
    var citys = ArrayList<ArrayList<GetRegionResponse>>()
    var areas = ArrayList<ArrayList<ArrayList<GetRegionResponse>>>()

    override fun getRegion(regionId: Int) {
        if (getRegionIsBusying) return
        getRegionIsBusying = true
        OperationManager.getInstancce().getRegion(regionId, object : OperateListener {
            override fun onSuccess(state: Int, obj: Any?) {
                getRegionIsBusying = false
                if (state == 200) {
                    val data = GsonBuilder().create().fromJson<ArrayList<GetRegionResponse>>(
                        GsonBuilder().create().toJson(obj), object : TypeToken<ArrayList<GetRegionResponse>>(){}.type)
                    parseAreas(data)
                }
            }

            override fun onFail(code: Int, message: String) {
                getRegionIsBusying = false
                mView?.getRegionFailed(code, message)
            }

        })
    }

    /**
     * 解析地区
     */
    private fun parseAreas(allAreas: ArrayList<GetRegionResponse>) {

        // 如果有缓存了，就不解析
        if(provinces.size > 0) return

        var provincesTags = hashMapOf<Int, Int>()
        var cityTags = hashMapOf<Int, Int>()
        var cityToProvinceTags = hashMapOf<Int, Int>()

        // 获取省
        for(area in allAreas) {
            if(area.parent_id == 1) {
                provincesTags[area.region_id] = areas.size
                provinces.add(area)
                citys.add(arrayListOf())
                areas.add(arrayListOf())
            }
        }

        // 获取市
        for(area in allAreas) {
            if(provincesTags[area.parent_id] != null) {
                cityTags[area.region_id] = citys[provincesTags[area.parent_id]!!].size
                cityToProvinceTags[area.region_id] = provincesTags[area.parent_id]!!

                citys[provincesTags[area.parent_id]!!].add(area)
                areas[provincesTags[area.parent_id]!!].add(arrayListOf())
            }
        }

        // 获取区
        for(area in allAreas) {
            if(cityTags[area.parent_id] != null) {
                var index = citys[cityToProvinceTags[area.parent_id]!!][cityTags[area.parent_id]!!]
                L.v(">>>>>>", "index: $index")
                areas[cityToProvinceTags[area.parent_id]!!][cityTags[area.parent_id]!!].add(area)
            }
        }

        // 回调给view
        mView?.getRegionSuccessed()

    }


    override fun addAddress(
        consignee: String,
        regionList: ArrayList<Int>,
        address: String,
        mobile: String,
        isDefault: Int
    ) {
        if (addAddressIsBusying) return
        addAddressIsBusying = true
        OperationManager.getInstancce().addAddress(consignee, regionList, address, mobile, isDefault, object : OperateListener {
            override fun onSuccess(state: Int, obj: Any?) {
                addAddressIsBusying = false
                if (state == 200) {
                    mView?.addAddressSuccessed()
                }
            }

            override fun onFail(code: Int, message: String) {
                addAddressIsBusying = false
                mView?.addAddressFailed(code, message)
            }

        })
    }

    override fun editAddress(
        userAddressId: Int,
        consignee: String,
        regionList: ArrayList<Int>,
        region: String,
        address: String,
        mobile: String,
        isDefault: Int
    ) {
        if (editAddressIsBusying) return
        editAddressIsBusying = true
        OperationManager.getInstancce().editAddress(userAddressId, consignee, regionList, region, address, mobile, isDefault,
        object : OperateListener {
            override fun onSuccess(state: Int, obj: Any?) {
                editAddressIsBusying = false
                if (state == 200) {
                    mView?.editAddressSuccessed()
                }
            }

            override fun onFail(code: Int, message: String) {
                editAddressIsBusying = false
                mView?.editAddressFailed(code, message)
            }

        })
    }

    override fun delAddress(userAddressIds: IntArray) {
        if (delAddressIsBusying) return
        delAddressIsBusying = true
        OperationManager.getInstancce().delAddress(userAddressIds, object : OperateListener {
            override fun onSuccess(state: Int, obj: Any?) {
                delAddressIsBusying = false
                if (state == 200) {
                    mView?.delAddressSuccessed()
                }

            }

            override fun onFail(code: Int, message: String) {
                delAddressIsBusying = false
                mView?.delAddressFailed(code, message)
            }

        })
    }

}